package com.android.data;

import java.util.ArrayList;

import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.android.R;
import com.android.entities.Category;
import com.android.entities.Event;
import com.android.entities.Group;

public class ServerManager {

	public static JSONObject getEvents(Context context, int startTime, String categories) {
		JSONObject result = new JSONObject();
		
    	String url = context.getString(R.string.webserviceURL) + "events?";
    	
    	ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    	BasicNameValuePair timeParam = new BasicNameValuePair("startTime", String.valueOf(startTime));
    	BasicNameValuePair categoriesParam = new BasicNameValuePair("categories", categories);
    	params.add(timeParam);
    	params.add(categoriesParam);
    
    	String urlParams = URLEncodedUtils.format(params, "utf-8");
    	String completeUrl = url + urlParams + "&filtered=true";
    	result = RestClient.getResponse(completeUrl);
    	
    	return result;
	}
	
	public static ArrayList<Category> getCategories(Context context) {
		ArrayList<Category> categories = new ArrayList<Category>();
		String url = context.getString(R.string.webserviceURL) + "categories";
		
		JSONObject categoriesObj = RestClient.getResponse(url);
		try {
			if (!categoriesObj.has("categories"))
				return categories;
			
			JSONArray categoriesArray = categoriesObj.getJSONArray("categories");
			for (int i = 0; i < categoriesArray.length(); i++) {
				JSONObject cObj = categoriesArray.getJSONObject(i);
				if (cObj.has("name")) {
					Category c = new Category(cObj.getString("name"));
					categories.add(c);
				}
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return categories;
	}
	
	public static ArrayList<Group> getCategoryGroups(Context context, String method) {
		ArrayList<Group> groups = new ArrayList<Group>();
		
		if (method == null || method.length() == 0)
			return groups;
		 
		String url = context.getString(R.string.webserviceURL) + "categories/" + method + "/events/0";
		
		JSONObject eventsObj = RestClient.getResponse(url);
		
		try {
			if (eventsObj.has("events")) {
				JSONArray eventsArray = eventsObj.getJSONArray("events");
				
				for (int i = 0; i <  eventsArray.length(); i++) {
					JSONObject eventObj = eventsArray.getJSONObject(i);
					
					Group eventGroup = new Group();
					eventGroup.setName(eventObj.getString("eventName"));
					
//					Children eventChildren = new Children();
//					if (eventObj.has("venues")) {
//						JSONArray venuesArray = eventObj.getJSONArray("venues");
//						
//						for (int j = 0; j < venuesArray.length(); j++) {
//							JSONObject venueObj = venuesArray.getJSONObject(j);
//							Child c = new Child();
//							c.setName(venueObj.getString("venueName"));
//							eventChildren.addChild(c);
//						}
//						
//					}
					//eventGroup.setChildren(eventChildren);
					groups.add(eventGroup);
				}
				
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return groups;
	}

	public static JSONObject getDetails(Context context, String eventSearchKey, String venueSearchKey) {		
		String url = context.getString(R.string.webserviceURL);
		url += "events/" + eventSearchKey;
		url += "/venues/" + venueSearchKey;
		
		JSONObject details = RestClient.getResponse(url);
		return details;
	}
	
	public static int createUserId(Context context) {
		String url = context.getString(R.string.webserviceURL);
		
		JSONObject userObject = RestClient.getResponse(url);
		
		try {
			if (userObject.has("userId") && userObject.getInt("userId") > 0)
				return userObject.getInt("userId");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return -1;		
	}
	
	public static ArrayList<Event> getRecommendations() {
		ArrayList<Event> events = new ArrayList<Event>();
		
		Event e = new Event();
		e.setName("Evento 1");
		e.setDescription("description");
		e.setId("event_1");
		e.setCategory("Comedy");
		events.add(e);

		Event e1 = new Event();
		e1.setName("Evento 2");
		e1.setDescription("description");
		e1.setId("event_2");
		e1.setCategory("Comedy");
		events.add(e1);

		Event e2 = new Event();
		e2.setName("Evento 3");
		e2.setDescription("description");
		e2.setId("event_3");
		e2.setCategory("Comedy");
		events.add(e2);

		Event e3 = new Event();
		e3.setName("Evento 3");
		e3.setDescription("description");
		e3.setId("event_4");
		e3.setCategory("Comedy");
		events.add(e3);

		return events;
	}
}
